package com.sunop.discussion.controller;


import com.sunop.discussion.model.Discussion;
import com.sunop.discussion.model.DiscussionPageQueryDTO;
import com.sunop.discussion.model.DiscussionReplyDTO;
import com.sunop.discussion.model.DiscussionSendDTO;
import com.sunop.discussion.service.DiscussionService;
import com.sunop.panticommon.entity.PageResult;
import com.sunop.panticommon.entity.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @since 2025-03-22
 */
@RestController
@RequestMapping("/discussion")
@AllArgsConstructor
public class DiscussionController {
    private final DiscussionService discussionService;

    @PostMapping("/send")
    public Result sendComment(@RequestBody DiscussionSendDTO discussionSendDTO) {
        return discussionService.sendComment(discussionSendDTO);
    }

    @GetMapping("/list")
    public Result<PageResult> listPageQuery(@RequestBody DiscussionPageQueryDTO discussionPageQueryDTO) {
        try {
            PageResult pageResult = discussionService.pageQuery(discussionPageQueryDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/{id}")
    public Result<Discussion> getDiscussionInfo(@PathVariable Integer id) {
        try {
            Discussion discussion = discussionService.getById(id);
            return Result.success(discussion);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        return discussionService.delById(id);
    }

    @PostMapping("/{discussionId}/like")
    public Result like(@PathVariable Integer discussionId) {
        return discussionService.discussionLike(discussionId);
    }

    @PostMapping("/{discussionId}/unlike")
    public Result unlike(@PathVariable Integer discussionId) {
        return discussionService.discussionUnlike(discussionId);
    }

    @PostMapping("/sendReply")
    public Result sendReply(@RequestBody DiscussionReplyDTO discussionSendDTO) {
        return discussionService.sendReply(discussionSendDTO);
    }

    @GetMapping("/listReply/{id}")
    public Result<PageResult> listReply(@PathVariable Integer id, @RequestBody DiscussionPageQueryDTO discussionPageQueryDTO) {
        try {
            PageResult pageResult = discussionService.replyPageQuery(discussionPageQueryDTO, id);
            return Result.success(pageResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/deleteReply")
    public Result deleteReply(@RequestParam("id") Integer id) {
        return discussionService.delReplyById(id);
    }

    // 收藏讨论帖
    @PostMapping("/{discussionId}/collect")
    public Result collect(@PathVariable Integer discussionId) {
        return discussionService.collectDiscussion(discussionId);
    }

    // 取消收藏
    @DeleteMapping("/{discussionId}/collect")
    public Result uncollect(@PathVariable Integer discussionId) {
        return discussionService.uncollectDiscussion(discussionId);
    }

    // 查看收藏列表（分页 + 排序）false 正序，true 倒序
    @GetMapping("/collect")
    public Result getCollectedDiscussions(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "false") boolean reverseOrder) {
        return discussionService.getCollectedDiscussions(page, size, reverseOrder);
    }

    // 获取收藏总数
    @GetMapping("/collect/count")
    public Result getCollectedCount() {
        long count = discussionService.getCollectedCount();
        return Result.success(count, "获取收藏总数成功");
    }

    // 检查是否已收藏
    @GetMapping("/{discussionId}/collect/status")
    public Result isCollected(@PathVariable Integer discussionId) {
        boolean isCollected = discussionService.isDiscussionCollected(discussionId);
        return Result.success(isCollected, "检查收藏状态成功");
    }

}
